package com.polaris.live.adapter.gift

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.asynclayoutinflater.view.AsyncLayoutInflater
import androidx.constraintlayout.widget.ConstraintLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.polaris.live.R
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterGiftWallBinding
import com.polaris.live.resp.back_resp.GiftBean
import com.polaris.live.utils.BitwiseUtils
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.image.loadGiftImage
import com.polaris.live.widget.refresh.BaseAsyncViewHolder

/**
 * GiftWallAdapter
 *
 * @author Created by 半仙 on 2023/9/18/018 18:48
 */
class GiftWallAdapter :
    BaseQuickAdapter<GiftBean, GiftWallAdapter.GiftViewHolder>(R.layout.adapter_gift_wall) {

    private var mSelectPosition = -1
    private var mSelectTimes = 1

    private var layoutInflater: AsyncLayoutInflater? = null

    private fun getLayoutInflater(): AsyncLayoutInflater {
        return layoutInflater ?: let {
            AsyncLayoutInflater(context).apply {
                layoutInflater = this
            }
        }
    }

    override fun onCreateDefViewHolder(parent: ViewGroup, viewType: Int): GiftViewHolder {
        return GiftViewHolder(context).apply {
            getLayoutInflater().inflate(R.layout.adapter_gift_wall, parent) { inflatedView, _, _ ->
                if (ViewUtils.isContextValid(context)) {
                    bindView(AdapterGiftWallBinding.bind(inflatedView))
                }
            }
        }
    }

    override fun convert(holder: GiftViewHolder, item: GiftBean) {
        holder.mSelectPosition = mSelectPosition
        holder.mSelectTimes = mSelectTimes

        holder.bind(item)
    }

    fun updateSelected(position: Int, times: Int) {
        mSelectPosition = position
        mSelectTimes = times
    }

    /**
     * 获取当前选中的礼物
     */
    fun getSelectGift(): GiftBean? {
        return data.getOrNull(mSelectPosition)
    }

    class GiftViewHolder(context: Context) :
        BaseAsyncViewHolder<AdapterGiftWallBinding, GiftBean>(context) {

        var mSelectPosition = -1
        var mSelectTimes = 1

        override fun bind(binding: AdapterGiftWallBinding, item: GiftBean) {
            binding.apply {
                val position = absoluteAdapterPosition
                ivFrame.setImageResource(0)
                if (position == mSelectPosition) {
                    giftWallSelectNum.visibility = View.VISIBLE
                    relGiftWallBack.setBackgroundResource(R.drawable.r8_with_fe1849)
                } else {
                    giftWallSelectNum.visibility = View.GONE
                    relGiftWallBack.background = null
                }
                giftWallSelectNum.text = "$mSelectTimes"

                giftWallName.text = item.giftName
                giftWallPrice.text = "${item.coin}"

                Glide.with(giftWallImg.context)
                    .loadGiftImage(item.pic)
                    .into(giftWallImg)

                if (BitwiseUtils.isBitwise(item.type, LiveConst.GiftType.LUCK_GIFT)) {
                    ivLuck.visibility = View.VISIBLE
                } else {
                    ivLuck.visibility = View.GONE
                }

                if (BitwiseUtils.isBitwise(item.type, LiveConst.GiftType.GAME_BEANS_GIFT)) {
                    ivBeansType.setImageResource(R.drawable.ic_game_beans)
                } else {
                    ivBeansType.setImageResource(R.drawable.ic_diamond1)
                }

                val payloadBean = item.payloadBean
                if (payloadBean != null) {
                    if (BitwiseUtils.isBitwise(
                            item.type,
                            LiveConst.GiftType.GIFT_PRIVILEGE
                        ) && payloadBean.type == LiveConst.GiftPrivilegeType.VIP
                    ) {
                        widgetVip.setVipLevel(payloadBean.businessId?.toLong(), 39.autoDp, 12.autoDp)
                        widgetVip.visibility = View.VISIBLE
                    }
                    val layout = giftWallImg.layoutParams as ConstraintLayout.LayoutParams
                    if (payloadBean.frame != null) {
                        layout.width = 30.autoDp
                        layout.height = 40.autoDp
                        Glide.with(root.context).load(ImageUtils.imgPath(payloadBean.frame)).into(ivFrame)
                    } else {
                        layout.width = 56.autoDp
                        layout.height = 56.autoDp
                    }
                    giftWallImg.layoutParams = layout
                } else {
                    widgetVip.visibility = View.GONE
                }
            }
        }
    }
}